package com.fzb.forum4j.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.fzb.forum4j.dao.api.ICommentDao;
import com.fzb.forum4j.po.Article;
import com.fzb.forum4j.po.Blog;
import com.fzb.forum4j.po.Comment;
import com.fzb.forum4j.po.QueryMode;
@Repository("commentDaoImpl")
public class CommentDaoImpl extends HibBaseImpl<Comment> implements ICommentDao {

	private static String BASICCOMMMSG="t.content as content,t.user.details.headerFileName as header, t.commentId as id,t.commTime as commTime,t.user.userName as userName";

	
	/** 利用递归实现楼中楼
	 * @param rid
	 * @param data
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> getCommentsByCommentIds(Integer rid, List<Map<String, Object>> data) {
		data=getSession().createQuery("select new map("+BASICCOMMMSG+") from Comment t where t.replyComm.commentId=:cid").setParameter("cid",rid).list();
		for (Map<String, Object> map : data) {
			List<Map<String,Object>> tlist=getCommentsByCommentIds((Integer) map.get("id"),data);
			if(!tlist.isEmpty()){
				map.put("reply", tlist);
			}
		}
		return data;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void getCommentsByCid(QueryMode<Article> qmd) {
		Long records=(Long) getSession().createQuery("select count(t.commentId) from Comment t where t.replyComm=null and t.area.cid=:cid").setParameter("cid", qmd.getId()).uniqueResult();
		if(records!=null){
			qmd.getData().put("records",records);
			qmd.getData().put("total",(int)Math.ceil(records/5.0));
			qmd.getData().put("page", qmd.getT().getCommentPage());
			List<Map<String, Object>> data=getSession().createQuery("select new map(t.content as content,t.commentId as id,t.commTime as commTime,t.user.userName as userName) from Comment t where t.replyComm=null and t.area.cid=:cid")
					.setParameter("cid", qmd.getId()).setFirstResult(getStartRecord(qmd.getT().getCommentPage())).setMaxResults(5).list();
			for (Map<String, Object> map : data) {
				List<Map<String,Object>> tlist=getCommentsByCommentIds((Integer) map.get("id"),data);
				if(!tlist.isEmpty()){
					map.put("reply", tlist);
				}
			}
			qmd.getData().put("comments", data);
		}
		
	}
	@SuppressWarnings("unchecked")
	@Override
	public void getBlogCommentsByCid(QueryMode<Blog> qmd) {
		Long records=(Long) getSession().createQuery("select count(t.commentId) from Comment t where t.replyComm=null and t.area.cid=:cid").setParameter("cid", qmd.getId()).uniqueResult();
		if(records!=null){
			
			qmd.getData().put("records",records);
			qmd.getData().put("total",(int)Math.ceil(records/5*1.0));
			qmd.getData().put("page", qmd.getT().getCommentPage());
			List<Map<String, Object>> data=getSession().createQuery("select new map("+BASICCOMMMSG+") from Comment t where t.replyComm=null and t.area.cid=:cid")
					.setParameter("cid", qmd.getId()).setFirstResult(getStartRecord(qmd.getT().getCommentPage())).setMaxResults(5).list();
			for (Map<String, Object> map : data) {
				List<Map<String,Object>> tlist=getCommentsByCommentIds((Integer) map.get("id"),data);
				if(!tlist.isEmpty()){
					map.put("reply", tlist);
				}
			}
			qmd.getData().put("comments", data);
			
		}
		
	}
	@Override
	public void getList(QueryMode<Comment> qmd) {
		QueryDataByQmd(qmd, BASICCOMMMSG, "1=1", "commentId");
	}
	
	@Override
	public void getListByFilters(QueryMode<Comment> qmd) {
		QueryDataByQmd(qmd, BASICCOMMMSG, qmd.getFilters(), "commentId");

	}
 

}
 